package com.openlogic.logic.service.datasource.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.handle.Context;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.openlogic.common.core.constants.CacheConstant;
import com.openlogic.common.core.exception.logic.RuleException;
import com.openlogic.common.redis.service.RedisService;
import com.openlogic.datasource.api.dto.SqlConsoleDto;
import com.openlogic.datasource.api.entity.DataTableEntity;
import com.openlogic.datasource.api.entity.table.Tables;
import com.openlogic.datasource.api.query.DataTableQuery;
import com.openlogic.datasource.api.vo.DataTableVo;
import com.openlogic.datasource.api.vo.SqlConsoleVo;
import com.openlogic.logic.mapper.datasource.DataTableMapper;
import com.openlogic.logic.service.datasource.DataTableService;
import com.openlogic.logic.service.datasource.SqlConsoleService;

import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: MetadataTableServiceImpl
 * @Description: 数据库表信息表 ServiceImpl业务实现层
 * @author weiyongpeng
 * @date 2024年11月13日 下午5:51:24
 */
@Component
public class DataTableServiceImpl extends ServiceImpl<DataTableMapper, DataTableEntity>
		implements DataTableService {
	
	private static final String sql = "select * from {0} ;";

	@Inject
	private DataTableMapper dataTableMapper;

	@Inject
	private SqlConsoleService sqlConsoleService;
	
	@Inject
	private RedisService redisService;

	@Override
	public DataTableVo getMetadataTableById(String id,String tableId) {
		DataTableVo dataTableVo = dataTableMapper.findMetadataTableById(id,tableId);
		return dataTableVo;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<DataTableEntity> getDataMetadataTableList(DataTableQuery dataTableQuery) {
		if (StrUtil.isBlank(dataTableQuery.getSourceId())) {
			throw new RuleException("数据源不能为空");
		}
		List<DataTableEntity> tableList = (List<DataTableEntity>) redisService
				.getCacheMapValue(CacheConstant.METADATA_TABLE_KEY, dataTableQuery.getSourceId(),DataTableEntity.class);
		Stream<DataTableEntity> stream = Optional.ofNullable(tableList).orElseGet(ArrayList::new).stream();
		List<DataTableEntity> list = stream.collect(Collectors.toList());
		return CollectionUtils.isEmpty(list)
				? StringUtils.isNotBlank(dataTableQuery.getTableName())
						? dataTableMapper.selectListByQuery(QueryWrapper.create().where(Tables.dataTable.tableName.eq(dataTableQuery.getTableName())))
						: dataTableMapper.selectListByQuery(QueryWrapper.create())
				: StringUtils.isNotBlank(dataTableQuery.getTableName())
						? list.stream().filter(table -> table.getTableName().equals(dataTableQuery.getTableName()))
								.collect(Collectors.toList())
						: list;
	}

	@Override
	public List<SqlConsoleVo> getTableDataList(DataTableQuery dataTableQuery) {
		String sqlValue = MessageFormat.format(sql, dataTableQuery.getTableName());
		Integer page = Context.current().paramAsInt("page", 1);
        Integer pageSize = Context.current().paramAsInt("pageSize", 10);
		// 这里要适配分页
		SqlConsoleDto sqlConsoleDto = new SqlConsoleDto(
			new Date().getTime()+"",
			dataTableQuery.getSourceId(),
			sqlValue, 
			dataTableQuery.getTableName(),
			page,
			pageSize
		);
		List<SqlConsoleVo> sqlRun = sqlConsoleService.execSql(sqlConsoleDto);
		return sqlRun;
	}

}